import {createRouter,createWebHistory} from 'vue-router'
import {unauthorized} from "@/net/index.js";
const router=createRouter({
    history:createWebHistory(import.meta.env.BASE_URL),
    routes:[
        {
            path:'/',
            name:'auth',
            component:()=>import('@/views/Auth/Auth.vue'),
            children:[
                {
                    path:'',
                    name:'auth-login',
                    component:()=>import('@/views/Auth/LoginPage.vue')
                },{
                    path:'register',
                    name:'auth-register',
                    component:()=>import('@/views/Auth/RegisterPage.vue')
                },{
                    path:'reset',
                    name:'auth-reset',
                    component:()=>import('@/views/Auth/ResetPage.vue')
                }
            ]
        },
        {
            path:'/index',
            name:'index',
            component:()=>import('@/views/index/Index.vue'),
            children:[{
                path:'',
                name:'index-container',
                component:()=>import('@/views/index/IndexContainer.vue')
            },{
                path:'home',
                name:'index-home',
                component:()=>import('@/views/index/IndexHome.vue')
            },{
                path:'forums/:id',
                name:'index-forums',
                component:()=>import('@/views/index/forums.vue')
            },{
                path:'admin',
                name:'index-admin',
                component:()=>import('@/views/index/IndexAdmin.vue')
            },
            ]
        }
    ]
})
router.beforeEach((to,form,next)=>{
    console.log('Navigating to:', to.name, 'Full path:', to.fullPath);
    const isUnauthorized=unauthorized()
    if(to.name.startsWith('auth-')&&!isUnauthorized){
        next('/index')
    }else if(to.fullPath.startsWith("/index")&&isUnauthorized){
        next('/')
    }else{
        next()
    }



})
export default router